package com.runa.rccprotocol.dto.debao;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.Node;
import com.runa.monitor.comm.dto.newComm.HotMeterVO;
import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.persistence.DateUtil;
import com.runa.persistence.task.RequestPendingTask;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.runa.RunaFrameSequence;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.runa.protocol.util.ByteUtil.byteToHexStringFlip;
import static com.runa.rccprotocol.analysis.RunaDeviceAnalysis.*;

/**
 * 2024/10/11 15:01
 * zhangYB
 */
@Data
public class DeBaoAnalysisData {

    private String netEquNo;

    private Date readTime;
    private Date equTime;
    private String accCold;
    private byte accColdUnit;
    private Long accHeat;
    private byte accHeatUnit;
    private String power;
    private byte powerUnit;
    private String flow;
    private byte flowUnit;
    private String accFlow;
    private byte accFlowUnit;
    private String supplyTemp;
    private String returnTemp;
    private String workTime;
    private boolean success = true;

    private Integer valveOpen;

    private String status;
    private Integer pn;

    private List<String> faultList = new ArrayList<>();

    public DeBaoAnalysisData(String netEquNo) {
        this.netEquNo = netEquNo;
    }


    public void analysisAutoRead(byte[] data) {


        pn = RunaFrameSequence.pn(data[14], data[15]);
        int index = 18;
//        readTime = ByteUtil.byteToHexStringFlip(data, index, 5);
        readTime = DateUtil.stringToDate(ByteUtil.byteToHexStringFlip(data, index, 5), "yyMMddHHmm");

        index = index + 5 + 2;
        accCold = ByteUtil.byteToHexStringFlip(data, index, 4);
        accColdUnit = data[index+4];
        index += 5;

        RequestPendingTask task = RequestPendingTask.getTask(netEquNo);
        if (null != task && null != task.getNode()) {
            Node node = task.getNode();
            String model = node.getModel();
            if ("HM-25-U/200".equalsIgnoreCase(model)) {
                accHeat = ByteUtil.byteToUInt(data, index, ByteOrder.DCBA);
            }
        }
        if (null == accHeat) {
            accHeat = Long.valueOf(ByteUtil.byteToHexStringFlip(data, index, 4));
        }
        accHeatUnit = data[index+4];
        index += 5;
        power = ByteUtil.byteToHexStringFlip(data, index, 4);
        powerUnit = data[index+4];
        index += 5;
        flow = ByteUtil.byteToHexStringFlip(data, index, 4);
        flowUnit = data[index+4];
        index += 5;
        accFlow = ByteUtil.byteToHexStringFlip(data, index, 4);
        accFlowUnit = data[index+4];
        index += 5;
        supplyTemp = ByteUtil.byteToHexStringFlip(data, index, 3);
        index += 3;
        returnTemp = ByteUtil.byteToHexStringFlip(data, index, 3);
        index += 3;
        workTime = ByteUtil.byteToHexStringFlip(data, index, 3);
        index += 3;
        String s = byteToHexStringFlip(data, index, 7);
        if (!s.contains("EE")) {
            equTime = DateUtil.noLineFormatToDate(s);
        }
        index += 7;
        status = ByteUtil.byteToHexString(data, index, 2);

        short sta = ByteUtil.byteToShort(data, index, ByteOrder.AB);

        if ((sta >> 7 & 1) == 1) {
            faultList.add("阀门故障");
        }
        if ((sta >> 8 & 1) == 1) {
            faultList.add("控制器故障");
        }
        if ((sta >> 11 & 1) == 1) {
            faultList.add("与控制器通讯故障");
        }
        if ((sta >> 14 & 1) == 1) {
            faultList.add("热量表故障");
        }

        index += 2;
        success = data[index] == 0x00;
        index = index + 1 + 3 + 3 + 4;

        valveOpen = data[index] == 0 ? 0 : 100;


    }

    public HotMeterVO getMeter() {
        HotMeterVO hotMeterVO = new HotMeterVO();
        hotMeterVO.setSuccess(this.success);
        hotMeterVO.setIndexNo(pn);
        hotMeterVO.setReadDate(readTime);
        hotMeterVO.setMeterDate(equTime);
        hotMeterVO.setFaultCode(faultList);
        hotMeterVO.setStatus(status);
        if (!this.success) {
            return hotMeterVO;
        }

        float accHeat = (float) (this.accHeat * 0.01);
        hotMeterVO.setAccHeat(toKwh(accHeat, accHeatUnit & 0xff));

        float power = (float) (Integer.parseInt(this.power) * 0.01);
        hotMeterVO.setPower(toKw(power, powerUnit & 0xff));

        float flow = (float) (Integer.parseInt(this.flow) * 0.0001);
        hotMeterVO.setFlow(toM3h(flow, flowUnit));

        float accFlow = (float) (Integer.parseInt(this.accFlow) * 0.01);
        hotMeterVO.setAccFlow(toM3(accFlow, accFlowUnit));

        hotMeterVO.setSupplyTemp((float) (Integer.parseInt(supplyTemp) * 0.01));
        hotMeterVO.setReturnTemp((float) (Integer.parseInt(returnTemp) * 0.01));

        hotMeterVO.setWorkTime(Integer.parseInt(workTime));

        return hotMeterVO;
    }


    public ValveVO getValve() {
        ValveVO valveVO = new ValveVO();
        valveVO.setSuccess(this.success);
        valveVO.setIndex(pn);
        valveVO.setReadTime(readTime);
        valveVO.setValveDate(equTime);
        valveVO.setFaultCode(faultList);
        valveVO.setStatus(status);
        if (!this.success) {
            return valveVO;
        }
        valveVO.setActOpen((float)valveOpen);
        return valveVO;
    }


    public static void main(String[] args) {
        String s = "685A015A01688862137A28020D600401401B00121210240301EEEEEEEEEEEEEEEEEEEEEEEEEEEE17EEEEEEEE35EEEEEEEE2CEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE00000100000000000000000000000000000000EE0000000000008516";
        byte[] bytes = ByteUtil.hexStringToByte(s);
        DeBaoAnalysisData deBaoAnalysisData = new DeBaoAnalysisData("");
        deBaoAnalysisData.analysisAutoRead(bytes);
        HotMeterVO meter = deBaoAnalysisData.getMeter();
        System.out.println(JSON.toJSONString(meter));

    }



}
